<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="类型" prop="type">
        <el-input v-model="dataForm.type" placeholder="类型"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="dataForm.description" placeholder="请描述"></el-input>
      </el-form-item>
      <el-form-item label="备注信息" prop="remarks">
        <el-input type="text" v-model="dataForm.remarks" placeholder="备注信息"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'notice-add',
  data() {
    return {
      visible: false,
      loading: false,
      fileList: [],
      dataForm: {
        id: '',
        type: '',
        description: '',
        remarks: ''
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.fileList = []
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (this.dataForm.id) {
        this.$http({
          url: '/dict/info',
          method: 'post',
          data: { id: this.dataForm.id }
        }).then(data => {
          if (data && data.code === 200) {
            this.dataForm.type = data.data.type
            this.dataForm.description = data.data.description
            this.dataForm.remarks = data.data.remarks
          }
        })
      }
    },
    dataFormSubmit() {
      this.loading = true
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: `/dict/${!this.dataForm.id ? 'save' : 'update'}`,
            method: 'post',
            data: {
              id: this.dataForm.id,
              type: this.dataForm.type,
              description: this.dataForm.description,
              remarks: this.dataForm.remarks
            }
          }).then(data => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.visible = false
              this.$emit('refreshDataList')
            } else {
              this.$message.error(data.msg)
            }
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
